import Vue from 'vue'
import Router from 'vue-router'
import Cookies from "js-cookie"
import Login from '@/components/views/Login'
// import NotFound from '@/components/sub/404'
import Home from '@/components/views/Home' // 首页
import TaskManagement from '@/components/views/TaskManagement' // 任务管理 
import NodeManagement from '@/components/views/NodeManagement' // 节点管理 
import DataManagement from '@/components/views/DataManagement' // 数据管理 
import AccountManage from '@/components/views/AccountManage' // 后台系统
import Help from '@/components/views/Help' // 帮助
import Test from '@/components/views/test'

Vue.use(Router)

const router = new Router({
  routes: [
    { path:'/', redirect: '/login' },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: { title: '首页' }
    },
    {
      path: '/taskManagement',
      name: 'TaskManagement',
      component: TaskManagement
    },
    {
      path: '/nodeManagement',
      name: 'NodeManagement',
      component: NodeManagement
    },
    {
      path: '/dataManagement',
      name: 'DataManagement',
      component: DataManagement
    },
    {
      path: '/test',
      name: 'Test',
      component: Test
    },
    {
      path: '/help',
      name: 'Help',
      component: Help
    },
    {
      path: '/accountManage',
      component: AccountManage,
      children:[
        {
          path: '/userManage',
          name: 'userManage',
          component: () =>
          import('@/components/views/userManage/userManage')
        },
        {
            path: '/roleManage',
            name: 'roleManage',
            component: () =>
                import('@/components/views/userManage/roleManage')
        },
        {
            path: '/departmentManage',
            name: 'departmentManage',
            component: () =>
                import('@/components/views/userManage/departmentManage')
        },
        {
          path: '/functionManage',
          name: 'functionManage',
          component: () =>
              import('@/components/views/userManage/functionManage.vue')
        },
        {
          path: '/menuManage',
          name: 'menuManage',
          component: () => 
            import('@/components/views/userManage/menuManage.vue')
        },
        {
            path: '/rightManage',
            name: 'rightManage',
            component: () =>
                import('@/components/views/userManage/rightManage')
        },
        {
            path: '/addUser',
            name: 'addUser',
            component: () =>
                import('@/components/views/userManage/addUser')
        },
        {
            path: '/dataManage',
            name: 'dataManage',
            component: () =>
                import('@/components/views/dataManage/dataManage.vue')
        },
        {
            path: '/dataBackup',
            name: 'dataBackup',
            component: () =>
                import('@/components/views/dataManage/dataBackup.vue')
        },
        // {
        //     path: '/dataBackup',
        //     name: 'dataBackup',
        //     component: () =>
        //         import('../components/accountSettings/dataBackup.vue')
        // }, 
       ] 
    },
    // {
    //   path: '/404',
    //   name: 'notFound',
    //   component: NotFound
    // }
  ]
})

router.beforeEach((to, from, next) => {
  // 登录界面登录成功之后，会把用户信息保存在会话
  // 存在时间为会话生命周期，页面关闭即失效。
  let token = Cookies.get('token')
  let userName = sessionStorage.getItem('user')
  let userId = sessionStorage.getItem('userId')
  if (to.path === '/login') {
    // 如果是访问登录界面，如果用户会话信息存在，代表已登录过，跳转到主页
    if(token) {
      sessionStorage.removeItem("user")
      sessionStorage.removeItem("userId")
      Cookies.remove("token")
      next()
    } else {
      next()
    }
  } else {
    if (!token) {
      // 如果访问非登录界面，且户会话信息不存在，代表未登录，则跳转到登录界面
      next({ path: '/login' })
    } else {
      next()
    }
  }
})

export default router